<?php if (!defined('BASEPATH')) exit('No direct script access allowed');

/**
 * 友情链接 控制器
 */
class Link extends Admin_Controller {

    private $_per_page = 10;

    public function __construct() {
        parent::__construct();
        $this->load->model('link_model');
        log_message('debug', 'Link Admin Controller Class Initialized');
    }

    /**
     * 列表页面
     */
    public function index() {
        if (IS_POST) {
            $posts = $this->input->post(null, true);
            foreach ($posts['data'] as $link_id => $post) {
                $save = array();
                $save['vieworder'] = (int)$post['vieworder'];
                $save['status'] = isset($post['status']) ? 1 : 0;
                $this->link_model->update($link_id, $save);
            }
            $this->show_success('success', 'admin/link');
        } else {
            $page = (int)$this->input->get('page', true);

            //分页
            $count = $this->link_model->count_all();
            $config['base_url'] = '?'; //基本url
            $config['per_page'] = $this->_per_page; //每页显示多少条数据
            $config['total_rows'] = $count;
            $this->load->library('pagination', $config);
            list($start, $limit) = Utility::page2limit($page, $this->_per_page);

            $data = array();
            $data['title'] = '友情链接';
            $data['page'] = $page;
            $data['links'] = $this->link_model->limit($limit, $start)->get_all();
            $this->load->view('link/link_index', $data);
        }
    }

    /**
     * 添加页面
     */
    public function add() {
        $this->load->library('form_validation');
        $this->form_validation->set_rules('name', '站点名称', 'required');
        $this->form_validation->set_rules('url', '站点地址', 'required|is_url');
        $this->form_validation->set_rules('logo', '站点LOGO', 'valid_url');
        if ($this->form_validation->run() == true) {
            $post = $this->input->post(null, true);

            $save = array();
            $save['name'] = $post['name'];
            $save['url'] = $post['url'];
            $save['logo'] = $post['logo'];
            $save['descrip'] = $post['descrip'];
            $save['contact'] = $post['contact'];
            $save['vieworder'] = (int)$post['vieworder'];
            $save['status'] = isset($post['status']) ? 1 : 0;
            $this->link_model->insert($save);
            $this->show_success('success', 'admin/link');
        } else {
            if ($error = $this->form_validation->get_error()) $this->show_error($error);

            $data = array();
            $data['title'] = '友情链接';
            $this->load->view('link/link_add', $data);
        }
    }

    /**
     * 编辑页面
     */
    public function edit() {
        $link_id = $this->input->get_post('link_id', true);
        $link = $this->link_model->get($link_id);
        if (!$link) $this->show_error('illegal_request');

        $this->load->library('form_validation');
        $this->form_validation->set_rules('name', '站点名称', 'required');
        $this->form_validation->set_rules('url', '站点地址', 'required|is_url');
        $this->form_validation->set_rules('logo', '站点LOGO', 'is_url');
        if ($this->form_validation->run() == true) {
            $post = $this->input->post(null, true);
            $post['link_id'] = (int)$post['link_id'];

            $save = array();
            $save['name'] = $post['name'];
            $save['url'] = $post['url'];
            $save['logo'] = $post['logo'];
            $save['descrip'] = $post['descrip'];
            $save['contact'] = $post['contact'];
            $save['vieworder'] = (int)$post['vieworder'];
            $save['status'] = isset($post['status']) ? 1 : 0;

            $this->link_model->update($post['link_id'], $save);
            $this->show_success('success', 'admin/link');

        } else {
            if ($error = $this->form_validation->get_error()) $this->show_error($error);

            $data = array();
            $data['title'] = '友情链接';
            $data['link'] = $link;
            $this->load->view('link/link_edit', $data);
        }
    }

    /**
     * 删除处理
     */
    public function dodel() {
        $link_id = (array)$this->input->get_post('link_id', true);
        if (empty($link_id)) $this->show_error('illegal_request');

        $this->link_model->delete_many($link_id);
        $this->show_success('success', 'admin/link');
    }
}

/* End of file admin/link.php */
/* Location: ./application/controllers/admin/link.php */
